package cn.trkj.client.controller;

import cn.trkj.client.util.DateUtils;
import cn.trkj.client.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author admin
 * @date 2021/2/20 3:30 下午
 */

@Controller
public class FileController {

    /**
     * 声明 restTemplate
     *
     * @return
     */
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }


    /**
     * 注入 restTemplate
     */
    @Autowired
    private RestTemplate restTemplate;


    /**
     * 服务器主机
     */
    public static String SERVER_DOMAIN = "http://183.93.33.149:9080/Server";
//    public static String SERVER_DOMAIN = "http://127.0.0.1:9080";

    /**
     * 构建 header，因为这段代码下面几个方法都需要调用，所以单独提取出来
     *
     * @return
     * @throws Exception
     */
    private HttpHeaders builderHeader() throws Exception {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setContentType(MediaType.parseMediaType("multipart/form-data; charset=UTF-8"));
        return headers;
    }


    /**
     * 文件上传
     *
     * @param
     * @return
     * @throws IOException
     */
    public void upload(MultipartFile multipartFile) throws Exception {
        HttpHeaders headers = builderHeader();
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(form, headers);
        InputStream ins = multipartFile.getInputStream();
        File file = new File(multipartFile.getOriginalFilename());
        FileUtil.inputStreamToFile(ins, file);
        FileSystemResource resource = new FileSystemResource(file);
        form.add("file", resource);
        try {
            // 向Server端请求上传
            restTemplate.exchange(SERVER_DOMAIN + "/file/upload", HttpMethod.POST, httpEntity, String.class);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            file.delete();
        }
    }


    @Scheduled(cron = "00 40 16 * * ?")
    public void uploadFile() {
        try {
            System.out.println("======开始执行========");
            String dateUtil = DateUtils.addDay1(new Date(), -1);
//            String dateUtil ="20231110";

//            File file1 = new File("F:\\" + dateUtil);
            //107 //东外环
            File file1 = new File("D:\\道路黑烟车智能电子抓拍系统\\dir\\" + dateUtil);
            if(null==file1){
                return;
            }
            ArrayList<File> objects = new ArrayList<File>();
            List<File> filesList = FileXh.getFilesList(file1, objects);
            for (File f : filesList) {
                if (f.getAbsolutePath().contains("mp4")) {
                    continue;
                }
                upload(FileXh.fileToMultipartFile(f));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("======结束执行========");
    }

}
